package com.payment.web.controller.tool;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.utils.URLEncodedUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.Image;
import com.aliyun.oss.common.utils.HttpUtil;
import com.payment.common.utils.DateUtils;
import com.payment.common.utils.ImageUtil;
import com.payment.common.utils.file.FileUtils;
import com.payment.common.utils.http.HttpClientUtil;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.Merchant;
import com.payment.system.domain.MerchantParent;
import com.payment.system.domain.MerchantWallet;
import com.payment.system.domain.PaymentOrder;
import com.payment.system.domain.SystemConfig;
import com.payment.system.mapper.MerchantMapper;
import com.payment.system.mapper.MerchantParentMapper;
import com.payment.system.mapper.MerchantWalletMapper;
import com.payment.system.mapper.PaymentOrderMapper;
import com.payment.system.mapper.SystemConfigMapper;
import com.payment.system.service.IMerchantService;
import com.payment.system.service.IMoneyChangeService;
import com.payment.system.service.IPaymentOrderService;
import com.payment.system.service.impl.WxAliPayServiceImpl;
import com.payment.system.util.GuangGuUtil;
import com.payment.system.vo.SettlementCardModifyVo;

/**
 * swagger 测试方法
 *
 * @author payment
 */
@Controller
@RequestMapping("/test")
public class TestController extends BaseController {
	@Autowired
	private WxAliPayServiceImpl wxAliPayService;
	@Autowired
	private IMoneyChangeService changeService;
	@Autowired
	private IPaymentOrderService orderService;
	@Autowired
	private SystemConfigMapper configMapper;
	@Autowired
	private IMerchantService merchantService;
	@Autowired
	private MerchantParentMapper merchantParentMapper;
	@Autowired
	private MerchantMapper merchantMapper;
	@Autowired
	private MerchantWalletMapper mWalletMapper;
	@Autowired
	private PaymentOrderMapper orderMapper;

	@RequestMapping("/test")
	@ResponseBody
	public String order() {
		SystemConfig config = configMapper.selectSystemConfigByConfigCode("commission");
		JSONObject jsonConfig = JSONObject.parseObject(config.getConfigValue());
		wxAliPayService.partnerCommission("15133177474", "11", jsonConfig, new BigDecimal("5000"));
		return "aa";
	}

	@RequestMapping("/duibi")
	@ResponseBody
	public String duibi(String url1, String url2) {
		String tokenS =HttpClientUtil.doPost(
				"https://aip.baidubce.com/oauth/2.0/token"
				+ "?grant_type=client_credentials"
				+ "&client_id=1b0wR6QfoDG8ASGyhKvHmoQd"
				+ "&client_secret=oq3CC35lZ46fCaAbq5Ddds3X4GZ1nOoY");
		JSONObject	jsonObject = JSONObject.parseObject(tokenS);
		String access_token = jsonObject.getString("access_token");
		return access_token;
	}
	
	@RequestMapping("/shibi")
	@ResponseBody
	public String shibi(String url1) throws UnsupportedEncodingException {
		String tokenS =HttpClientUtil.doPost(
				"https://aip.baidubce.com/oauth/2.0/token"
				+ "?grant_type=client_credentials"
				+ "&client_id=CVtRG61515dvftNDpCTciGy5"
				+ "&client_secret=exAHXGzRtQAItrhx2hcxzUzLxbO57vqW");
		JSONObject	jsonObject = JSONObject.parseObject(tokenS);
		String access_token = jsonObject.getString("access_token");
		Map<String,String> map = new HashMap<>();
		String base64 =ImageUtil.NetImageToBase64("https://myyxb-oss.oss-cn-beijing.aliyuncs.com/shouchi2.jpg");
		System.out.println(base64);
		map.put("image",base64);
		map.put("id_card_side", "front");
		String aaa =HttpClientUtil.doPost(
				"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
				+ "?access_token="+access_token,map);
		JSONObject	jsonObject1 = JSONObject.parseObject(aaa);
		return jsonObject1.toJSONString();
	}
	
	@RequestMapping("/duibia")
	@ResponseBody
	public String duibia(String url1, String url2) {
		String tokenS =HttpClientUtil.doPost(
				"https://aip.baidubce.com/oauth/2.0/token"
				+ "?grant_type=client_credentials"
				+ "&client_id=1b0wR6QfoDG8ASGyhKvHmoQd"
				+ "&client_secret=oq3CC35lZ46fCaAbq5Ddds3X4GZ1nOoY");
		JSONObject	jsonObject = JSONObject.parseObject(tokenS);
		String access_token = jsonObject.getString("access_token");
		//
		List<Map<String, String>> limap = new ArrayList<Map<String,String>>();
		Map<String, String> m1 = new HashMap<String, String>();
		m1.put("image", url1);
		m1.put("image_type", "URL");
		m1.put("face_type", "LIVE");
		limap.add(m1);
		Map<String, String> m2 = new HashMap<String, String>();
		m2.put("image", url2);
		m2.put("image_type", "URL");
		m2.put("face_type", "CERT");
		limap.add(m2);
		//m1.put("quality_control", value);
		System.out.println(JSONObject.toJSONString(limap));
		String a =HttpClientUtil.doPostJson("https://aip.baidubce.com/rest/2.0/face/v3/match"
				+ "?access_token="+access_token, JSONObject.toJSONString(limap));
		JSONObject	jsonObject1 = JSONObject.parseObject(a);
		return jsonObject1.toJSONString();
	}
	
	
	@RequestMapping("/zhuce")
	@ResponseBody
	public String zhuce(String url1) {
		String tokenS =HttpClientUtil.doPost(
				"https://aip.baidubce.com/oauth/2.0/token"
				+ "?grant_type=client_credentials"
				+ "&client_id=7GxSUqdVjGxuiW7krW6myvHj"
				+ "&client_secret=kdAxhhpcCPXRi3CuOBvikP0K05SW3s7G");
		JSONObject	jsonObject = JSONObject.parseObject(tokenS);
		String access_token = jsonObject.getString("access_token");
		//
		Map<String, String> m1 = new HashMap<String, String>();
		m1.put("image", "https://myyxb-oss.oss-cn-beijing.aliyuncs.com/shouchi.jpg");
		m1.put("image_type", "URL");
		m1.put("face_type", "LIVE");
		m1.put("group_id", "18032898345");
		m1.put("user_id", "81AECA690EE14787941F6A577ABC4754");
		System.out.println(JSONObject.toJSONString(m1));
		String a =HttpClientUtil.doPostJson("https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add"
				+ "?access_token="+access_token, JSONObject.toJSONString(m1));
		JSONObject	jsonObject1 = JSONObject.parseObject(a);
		return jsonObject1.toJSONString();
	}
	

	@RequestMapping("/settlementCardModify")
	@ResponseBody
	public String settlementCardModify() {
		SettlementCardModifyVo cardModifyVo = new SettlementCardModifyVo();
		cardModifyVo.setUserId("15756814715158129633");
		cardModifyVo.setUserName("付志莉");
		cardModifyVo.setUserdIdCard("130131199310162735");
		cardModifyVo.setBankCardNo("6217000010086959536");
		cardModifyVo.setBankName("建设银行");
		cardModifyVo.setBankCode("105");
		cardModifyVo.setBankAbbr("CCB");
		cardModifyVo.setBankChannelNo("105100000017");
		cardModifyVo.setBankProvince("北京市");
		cardModifyVo.setBankCity("北京市");
		cardModifyVo.setBankCardPhone("15133177474");

		try {
			JSONObject jsonObject = GuangGuUtil.settlementCardModify(cardModifyVo);
			return "结算卡变更==" + jsonObject.toJSONString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	
	// 调整上下级关系
	@RequestMapping("/updateMerchantParent")
	@ResponseBody
	public void updateMerchantParent(String phone, String parPhone) {
		merchantMapper.updateParentPhoneByPhone(parPhone,phone); 
		List<String> lowerPhones = merchantParentMapper.selectPhoneListByParentPhone(phone);
		lowerPhones.add(phone);
		merchantParentMapper.deleteMerchantParentByParentPhonOrPhone(phone);
		for (String lowerPhone : lowerPhones) {
			Merchant LowerMerchant = merchantService.selectByPhone(lowerPhone);
			String parentPhone = LowerMerchant.getParentPhone();
			int i = 1;
			while (true) {
				Merchant parentMerchant = merchantService.selectByPhone(parentPhone);
				if (parentMerchant == null) {
					break;
				}
				MerchantParent merchantParent = new MerchantParent();
				merchantParent.setMerchantPhone(lowerPhone);
				merchantParent.setParentPhone(parentPhone);
				merchantParent.setIntervals(i);
				merchantParentMapper.insertMerchantParent(merchantParent);
				i++;
				parentPhone = parentMerchant.getParentPhone();
			}
		}
	}
	
	
	/**
	 * 金领
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/jin")
	@ResponseBody
	public String jin() throws IOException {
		ClassPathResource resource = new ClassPathResource("s.json");
		File file = resource.getFile();
		String jsonString = FileUtils.readFileToString(file);
		JSONArray jl = JSONObject.parseArray(jsonString);
		List<LinkedHashMap<String, Object>> linew=new ArrayList<LinkedHashMap<String,Object>>();
		for (int i = 0; i < jl.size(); i++) {
			JSONObject json = jl.getJSONObject(i);
			MerchantWallet mw = mWalletMapper.selectMerchantWalletByMerId(json.getString("mid"));
			Integer [] orderTypes= {3,11};
			List<PaymentOrder> li=orderMapper.selectPaymentOrderListByOrderType(json.getString("mid"),orderTypes);
			MerchantWallet newMw = new MerchantWallet();
			newMw.setId(mw.getId());
			newMw.setVipMoney(BigDecimal.valueOf(320));
			newMw.setSkNum(li!=null?li.size():0);
			if (newMw.getSkNum()>=20 && newMw.getSkNum()<40){
				newMw.setVipMoney(newMw.getVipMoney().subtract(BigDecimal.valueOf(40)));
				newMw.setReleaseVipMoney(BigDecimal.valueOf(40));
				newMw.setTotalMoney(mw.getTotalMoney().add(BigDecimal.valueOf(40)));
				newMw.setBalanceMoney(mw.getBalanceMoney().add(BigDecimal.valueOf(40)));
			}
			mWalletMapper.updateMerchantWallet(newMw);
		}
		return "成功";
	}
	
	public static void main(String[] args) {
		List<String> timeList1 = DateUtils.findPlanDates("2020-01-29");
		for (int i = 0; i < timeList1.size(); i++) {
			System.out.println("one{" + timeList1.get(i) + "}");
		}
	}
}